package com.ruoyi.word.service;

import com.ruoyi.common.domain.word.BaiduWord;
import com.ruoyi.common.domain.word.EcDict;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class BaiduWordService {

    private static final Logger logger = LoggerFactory.getLogger(BaiduWordService.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存对象
     * @param book
     * @return
     */
    public String saveObj(BaiduWord book) {
        book.setCreateTime(new Date());
        book.setUpdateTime(new Date());
        mongoTemplate.save(book);
        return "添加成功: " + book.getSrc();
    }

    /**
     * 查询所有
     * @return
     */
    public List<BaiduWord> findAll() {
        return mongoTemplate.findAll(BaiduWord.class);
    }

    /***
     * 根据id查询
     * @param id
     * @return
     */
    public BaiduWord getById(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        return mongoTemplate.findOne(query, BaiduWord.class);
    }

    /**
     * 根据名称查询
     *
     * @param name
     * @return
     */
    public BaiduWord getByName(String name) {
        Query query = new Query(Criteria.where("src").is(name));
        return mongoTemplate.findOne(query, BaiduWord.class);
    }

    /**
     * 更新对象
     *
     * @param book
     * @return
     */
    public String updateBook(BaiduWord book) {
        // Query query = new Query(Criteria.where("_id").is(book.getId()));
        // Update update = new Update().set("publish", book.getPublish()).set("info", book.getInfo()).set("updateTime", new Date());
        // updateFirst 更新查询返回结果集的第一条
        // mongoTemplate.updateFirst(query, update, BaiduWord.class);
        // updateMulti 更新查询返回结果集的全部
        // mongoTemplate.updateMulti(query,update,Book.class);
        // upsert 更新对象不存在则去添加
        // mongoTemplate.upsert(query,update,Book.class);
        return "success";
    }

    /***
     * 删除对象
     * @param book
     * @return
     */
    public String deleteBook(BaiduWord book) {
        if(book == null) {
            return "nof_found";
        }
        mongoTemplate.remove(book);
        return "success";
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    public String deleteById(String id) {
        // findOne
        BaiduWord book = getById(id);
        if(book == null) {
            return "nof_found";
        }
        // delete
        deleteBook(book);
        return "success";
    }

    /**
     * 模糊查询
     * @param search
     * @return
     */
    public List<BaiduWord> findByLikes(String search){
        Query query = new Query();
        Criteria criteria = new Criteria();
        //criteria.where("name").regex(search);
        Pattern pattern = Pattern.compile("^.*" + search + ".*$" , Pattern.CASE_INSENSITIVE);
        criteria.where("src").regex(pattern);
        List<BaiduWord> lists = mongoTemplate.find(query, BaiduWord.class);
        return lists;
    }

    public List<EcDict> findAllEcDict() {return mongoTemplate.findAll(EcDict.class);}

    public String removeDuplicateData(){
        List<BaiduWord> all = findAll();
        all.forEach(w -> {
            List<BaiduWord> likes = findByLikes(w.getSrc());
            likes.forEach(l -> {
                if(l.getId().longValue() != w.getId()) {
                    deleteById(l.getId().toString());
                }
            });
        });
        return "ok";
    }

}